/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.随机数索引;

import java.util.HashMap;
import java.util.LinkedList;

public class Solution {
    HashMap<Integer, LinkedList<Integer>> data = new HashMap<>();

    public Solution(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            LinkedList<Integer> integers = data.getOrDefault(nums[i], new LinkedList<>());
            integers.add(i);
            data.put(nums[i], integers);
        }
    }

    public int pick(int target) {
        LinkedList<Integer> linkedList = data.get(target);
        linkedList.addFirst(linkedList.pollLast());
        return linkedList.peekFirst();
    }
}
